package algo.queue.impl;

import com.fay.algo.queue.impl.ArrayQueue;
import com.fay.algo.queue.impl.LinkedListQueue;
import com.fay.algo.queue.impl.LoopQueue;
import com.fay.algo.queue.impl.Queue;

import java.util.Random;

/**
 * @Title:
 * @Description:
 * @Author Fay Wang
 * @Date 2022/4/27 10:46
 * @Version 1.0
 */
public class QueueTest {
    public static void main(String[] args) {
        /*checkArrayQueue();
        checkLinkedListQueue();
        checkLoopQueue();*/
        compareQueuesTimeCount();
    }

    private static void compareQueuesTimeCount() {
        int optCount = 10000000;
        Queue<Integer> arrayQueue = new ArrayQueue<>();
        double time1 = testQueue(arrayQueue, optCount);
        System.out.println("ArrayQueue,time:" + time1 + "s");

        Queue<Integer> loopQueue = new LoopQueue<>();
        double time2 = testQueue(loopQueue, optCount);
        System.out.println("LoopQueue,time:" + time2 + "s");
    }

    // 测试使用q运行opCount个enqueue和dequeue操作所需要的时间，单位：秒
    private static double testQueue(Queue<Integer> queue, int opCount) {
        long startTime = System.nanoTime();
        Random random = new Random();
        for (int i = 0; i < opCount; i++) {
            queue.enqueue(random.nextInt(Integer.MAX_VALUE));
        }
        for (int i = 0; i < opCount; i++) {
            queue.dequeue();
        }
        long endTime = System.nanoTime();
        return (endTime - startTime) / 10e+9;
    }

    private static void checkLoopQueue() {
        Queue<Integer> queue = new LoopQueue<>();
        checkQueue(queue);
    }

    private static void checkQueue(Queue<Integer> queue) {
        for (int i = 0; i < 10; i++) {
            queue.enqueue(i);
            System.out.println(queue);
            if (i % 3 == 0) {
                queue.dequeue();
                System.out.println(queue);
            }
        }
    }

    private static void checkLinkedListQueue() {
        Queue<Integer> queue = new LinkedListQueue<>();
        checkQueue(queue);
    }

    private static void checkArrayQueue() {
        Queue<Integer> queue = new ArrayQueue<>();
        checkQueue(queue);
    }
}
